<template>
  <!-- 富文本 -->
  <div>
    <editor v-model="content" :init="init" :disabled="disabled"></editor>
  </div>
</template>

<script>
// import '@/utils/tinymce/tinymce.js'
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
// import "../../../public/static/tinymce/icons/default/icons";
// import "../../../public/static/tinymce/themes/silver";
// import "../../../public/static/tinymce/plugins/image";
// import "../../../public/static/tinymce/plugins/media";
// import "../../../public/static/tinymce/plugins/table";
// import "../../../public/static/tinymce/plugins/lists";
// import "../../../public/static/tinymce/plugins/contextmenu";
// import "../../../public/static/tinymce/plugins/wordcount";
// import "../../../public/static/tinymce/plugins/colorpicker";
// import "../../../public/static/tinymce/plugins/textcolor";
// import "../../../public/static/tinymce/plugins/preview";
// import "../../../public/static/tinymce/plugins/code";
// import "../../../public/static/tinymce/plugins/link";
// import "../../../public/static/tinymce/plugins/advlist";
// import "../../../public/static/tinymce/plugins/codesample";
// import "../../../public/static/tinymce/plugins/hr";
// import "../../../public/static/tinymce/plugins/fullscreen";
// import "../../../public/static/tinymce/plugins/textpattern";
// import "../../../public/static/tinymce/plugins/searchreplace";
// import "../../../public/static/tinymce/plugins/autolink";
// import "../../../public/static/tinymce/plugins/directionality";
// import "../../../public/static/tinymce/plugins/visualblocks";
// import "../../../public/static/tinymce/plugins/visualchars";
// import "../../../public/static/tinymce/plugins/template";
// import "../../../public/static/tinymce/plugins/charmap";
// import "../../../public/static/tinymce/plugins/nonbreaking";
// import "../../../public/static/tinymce/plugins/insertdatetime";
// import "../../../public/static/tinymce/plugins/imagetools";
// import "../../../public/static/tinymce/plugins/autosave";
// import "../../../public/static/tinymce/plugins/autoresize";
// import "../../../public/static/tinymce/plugins/kityformulaEditor/plugin"
//   import "../../../public/static/tinymce/plugins/kityformulaEditor/plugin"
import ZN from "./zh_CN";
import { upLoad } from "@/api/studentEnd/analysisOfTheStudents";
export default {
  components: {
    Editor,
  },
  props: {
    value: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    plugins: {
      type: [String, Array],
      default:
        "kityformula-editor preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr nonbreaking insertdatetime advlist lists wordcount imagetools textpattern autosave autoresize",
    },
    toolbar: {
      type: [String, Array],
      default:
        " kityformula-editor code undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link codesample | alignleft aligncenter alignright alignjustify outdent indent formatpainter | \
      styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
      table image media charmap hr pagebreak insertdatetime | fullscreen preview ",
    },
    isDisplay: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      //初始化配置
      init: {
        menubar: false, // 菜单栏显隐
        language_url: ZN,
        language: "zh_CN",
        //   skin_url: "/static/tinymce/skins/ui/oxide",
        height: "100%",
        min_height: "100%",
        // automatic_uploads: false,
        automatic_uploads: true, //图片自定义上传使用
        max_height: "100%",
        // inline: true,
        toolbar_mode: "wrap",
        plugins: this.plugins,
        branding: false,
        // toolbar: false,
        toolbar: this.toolbar,
        content_style: "p {margin: 5px 0;}",
        fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
        font_formats:
          "微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;",
        branding: false,
        // 图片上传
        images_upload_handler: (blobInfo, success, failure) => {
          // const img = 'data:image/jpeg;base64,' + blobInfo.base64()
          // success(img)
          console.log("图片上传", blobInfo, success, failure);

          const formData = new FormData();
          formData.append("file", blobInfo.blob());
          upLoad(formData)
            .then((res) => {
              if (res.code == "200") {
                const file = res;
                success(file.url + "?imageView2/2/w/700");
                return;
              }
              failure("上传失败");
            })
            .catch(() => {
              failure("上传出错");
            });
        },
        setup: (editor) => {
          if (!this.isDisplay) return;
          editor.on("focus", (e) => {
            editor.container.childNodes[0].childNodes[0].style.display =
              "block";
          });
          editor.on("blur", (e) => {
            editor.container.childNodes[0].childNodes[0].style.display = "none";
          });
        },
        kityformula_options: {
          formula_type: "letax", // 设置公式类型为letax
        },
      },
      content: this.value,
      editor: "",
      isTaber: true,
    };
  },
  mounted() {
    tinymce.init({});
  },
  methods: {},
  watch: {
    value(newValue) {
      this.content = newValue;
    },
    content(newValue) {
      this.$emit("input", newValue);
    },
  },
};
</script>
<style scoped lang="scss"></style>
